﻿#include <thread>
#include "Log/SystemLog.h"
#include "CircleBufferTest.h"

using namespace SystemTool::Buffer;

struct MyQueueStr
{
	int iAge;

	char* strMsg;
};

int TestCircleBuffer()
{
	SET_HOW_MUCH_RECORD_TO_FILE_ONE_TIME(1);

	CircleBuffer<MyQueueStr> MyBuffer(1024);

	std::thread t1 = std::thread([&MyBuffer]() {
		
		int iCount = 0;

		while (true)
		{
			MyQueueStr mydata;

			mydata.iAge = 20 + iCount++;

			mydata.strMsg = "very good1";

			MyBuffer.Push(mydata);

			std::string strMsg = std::string("Thread1:") + std::to_string(mydata.iAge);

			LOG_INFO_EX(strMsg);

			std::this_thread::sleep_for(std::chrono::milliseconds(500));
		}
	});

	std::thread t2 = std::thread([&MyBuffer]() {

		while (true)
		{
			int iCount = 0;

			while (true)
			{
				MyQueueStr mydata;

				MyBuffer.Fetch(mydata);

				std::string strMsg = std::string("Thread2:") + std::to_string(mydata.iAge);

				LOG_INFO_EX(strMsg);

				std::this_thread::sleep_for(std::chrono::milliseconds(1000));
			}
		}
	});

	if (t1.joinable())
	{
		t1.join();
	}

	if (t2.joinable())
	{
		t2.join();
	}

	return 0;
}
